package com.yxb.util;

public class CHexConver {


    /**
     * 将字符串转换为 字节数组
     * @param hexString
     * @return
     */
    public static short[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        short [] d = new short[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] =  (short)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     *
     * @param c
     * @return
     */
    private static byte  charToByte(char c){
        return (byte) "0123456789ABCDEF".indexOf(c);
    }


}
